<template>
  <el-drawer v-model="isShow" size="40%" :title="title" direction="rtl" :before-close="handleClose">
    <hr>
    <el-tabs v-model="activeName" class="demo-tabs">
      <el-tab-pane label="基础信息" name="first"></el-tab-pane>
      <el-tab-pane label="详细信息" name="second"></el-tab-pane>
    </el-tabs>
    <el-form :model="formData" ref="form" :rules="rules">
      <div v-show="activeName === 'first'">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item prop="name">
              <label style="width: 100%;"><span class="red">*</span>小区名称</label>
              <el-input v-model="formData.name" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="address">
              <label style="width: 100%;"><span class="red">*</span>坐落地址</label>
              <el-input v-model="formData.address" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item prop="area">
              <label style="width: 100%;"><span class="red">*</span>占地面积</label>
              <el-input v-model="formData.area" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="developer">
              <label style="width: 100%;"><span class="red">*</span>开发商名称</label>
              <el-input v-model="formData.developer" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item prop="estate">
              <label style="width: 100%;"><span class="red">*</span>物业公司名称</label>
              <el-input v-model="formData.estate" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="greening_rate">
              <label style="width: 100%;"><span class="red">*</span>绿化率</label>
              <el-input v-model="formData.greening_rate" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item prop="total_building">
              <label style="width: 100%;"><span class="red">*</span>总栋数</label>
              <el-input-number v-model="formData.total_building" class="mx-4" :min="0" controls-position="right" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="total_owner">
              <label style="width: 100%;"><span class="red">*</span>总户数</label>
              <el-input-number v-model="formData.total_owner" class="mx-4" :min="0" controls-position="right" />
            </el-form-item>
          </el-col>
        </el-row>
      </div>
      <!-- ==========================================tab切换页面======================================= -->
      <div v-show="activeName === 'second'">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item>
              <label style="width: 100%;">小区图片</label>
              <!-- 图片组件 -->
              <uploadImg v-model:imgUrl="formData.thumb" :limit="3" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="position">
              <label style="width: 100%;"><span class="red">*</span>经纬度</label>
              <div style="display: flex;flex-wrap: nowrap;"><el-input v-model="formData.position" readonly /> <el-button
                  type="primary" @click="dialogVisible = true">点击获取</el-button></div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <label style="width: 100%;">详细描述</label>
          <!-- 富文本组件 -->
          <editor :value="formData.introduction" :min-height="192" @update:input='changeInput' />
        </el-form-item>
      </div>

      <div style="display: flex;justify-content: end;margin-top: 50px;">
        <el-form-item>
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="onSubmit">确定</el-button>
        </el-form-item>
      </div>
    </el-form>
    <el-dialog v-model="dialogVisible" width="80%">
      <!-- 地图组件 -->
      <MapContainer @changePosition="changePosition" />
    </el-dialog>

  </el-drawer>
</template>

<script setup>
import { ref } from 'vue'
import { addListApi, getListInfoApi, editListApi } from '@/api/community/index.js'
import uploadImg from '@/components/Table/uploadImg.vue'
import MapContainer from '@/components/Table/MapContainer.vue'
import { ElMessage } from 'element-plus'
import editor from '@/components/Table/Editor.vue'
// =============================================================================
const isShow = ref(false)
const form = ref(null)
const activeName = ref('first')
// 地图弹窗
const dialogVisible = ref(false)
const title = computed(() => formData.value.id ? '修改小区' : '添加小区')
const defaultFormData = {
  address: "",
  area: "",
  developer: "",
  estate: "",
  greening_rate: "",
  introduction: "",
  name: "",
  position: "",
  thumb: "",
  total_building: 0,
  total_owner: 0,
}
const rules = {
  address: [
    { required: true, message: '请选择', trigger: 'blur' },
  ],
  area: [
    { required: true, message: '请选择', trigger: 'blur' },
  ],
  developer: [
    { required: true, message: '请选择', trigger: 'blur' },
  ],
  estate: [
    { required: true, message: '请输入', trigger: 'blur' },
  ],
  greening_rate: [
    { required: true, message: '请选择', trigger: 'blur' },
  ],
  name: [
    { required: true, message: '请选择', trigger: 'blur' },
  ],
  position: [
    { required: true, message: '请选择', trigger: 'change' },
  ],
  total_building: [
    { required: true, message: '请选择', trigger: 'blur' },
  ],
  total_owner: [
    { required: true, message: '请选择', trigger: 'blur' },
  ],
}
// 表单数据
let formData = ref({ ...defaultFormData })
const emit = defineEmits(['initData'])
// ==========================================================================

// 富文本
const changeInput = (val) => {
  // console.log(val);
  formData.value.introduction = val
}

// 获取坐标
const changePosition = (ev) => {
  // console.log(ev);
  formData.value.position = ev
}


// 添加=======修改
const onSubmit = async () => {
  try {
    await form.value.validate()
    formData.value.id ? await editListApi(formData.value) : await addListApi(formData.value)
    ElMessage.success(formData.value.id ? '修改成功' : '添加成功')
    isShow.value = false
    emit('initData')
  } catch (err) {
    console.log(err);
    ElMessage.error('必填项输入后才能提交喔')

  }
}

// 抽屉关闭前的回调
const handleClose = () => {
  form.value.resetFields()
  formData.value = { ...defaultFormData }
  isShow.value = false
}

// 修改回显
const edit = async (id) => {
  // console.log(id);
  const { data } = await getListInfoApi(id)
  // console.log(data);
  formData.value = data
  isShow.value = true
}
defineExpose({ isShow, edit })
</script>

<style lang="scss" scoped>
.red {
  color: red;
}
</style>